package com.deyuanyun.pic.settings.task;

import com.deyuanyun.pic.settings.service.msgtemplate.AbstractPushMsgTemplate;
import com.deyuanyun.pic.settings.service.msgtemplate.PipeCyclePushMsg;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;

import java.util.HashMap;
import java.util.Map;

/**
 *  管道周期提醒任务调度
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-07-20
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
public class PipeLineCycleTask extends MethodInvokingJobDetailFactoryBean.StatefulMethodInvokingJob {

    private static final Logger LOGGER = LoggerFactory.getLogger(PipeLineCycleTask.class);

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        try {
            if(LOGGER.isInfoEnabled()) {
                JobKey jobKey = context.getTrigger().getJobKey();
                LOGGER.info("得到需要执行的任务名为：" + jobKey.getName());
                LOGGER.info("得到需要执行的任务组名为："  + jobKey.getGroup());
                LOGGER.info("***管道周期提醒任务调度开始执行***");
            }
            JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
            Object scheduleObj = jobDataMap.get(PipeCyclePushMsg.SCHEDULE_VO); //得到需要提醒的管道信息
            Object pipeScheduleObj = jobDataMap.get(PipeCyclePushMsg.PIPE_SCHEDULE); //得到需要推送的业务阀值信息
            if(scheduleObj != null && pipeScheduleObj != null) {
                LOGGER.info("已经得到需要通知的管道信息和发送消息必须的业务阀值设置信息");
                AbstractPushMsgTemplate pipeCyclePushMsg = new PipeCyclePushMsg();
                Map<String, Object> map = new HashMap<String, Object>();
                map.put(PipeCyclePushMsg.PIPE_SCHEDULE, pipeScheduleObj);
                map.put(PipeCyclePushMsg.SCHEDULE_VO, scheduleObj);
                pipeCyclePushMsg.setInterface(false, map);
                pipeCyclePushMsg.pushMsg();
                LOGGER.info("要发送的消息封装结束，等待推送");
            }
            LOGGER.info("***管道周期提醒任务调度执行结束***");
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
}
